from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from OpenGL.GLE import *
from math import *

def sumaVec(A,B):
	s = []
	for i in range(len(A)):
		s.append(A[i]+B[i])
	return s
		
def restaVec(A,B):
	r = []
	for i in range(len(A)):
		r.append(A[i]-B[i])
	return r

def prodPunto(A,B):
	p = 0
	for i in range(len(A)):
		p = p + (A[i]*B[i])
	return p
	
theta = 30

class Figura:	
	def __init__(self):
		self.planos=[]
		self.mcolor = [0.8,0.5,0.8,0]	

	
	#def __planeNormal(self):
		#self.normPlane = []
		#for plano in self.planos:
		calcular la norma a cada uno de los planos
			
		
	def draw(self):
		glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, self.mcolor)
		for plano in self.planos:
			glBegin(GL_POLYGON)
			for punto in plano:
				glVertex3f(punto[0],punto[1],punto[2])
			glEnd()
			
class Cilindro:
	def __init__(self,radio,altura,slices):
		self.radio = radio
		self.altura = altura
		self.slices = slices
		self.fig = Figura()
		self.__calcular__()
		
	def __calcular__(self):
		arriba = []
		abajo = []
		theta = 0
		dtheta = ((2*pi)/(self.slices))
		for i in range(self.slices):
			x = self.radio*cos(theta)
			y = self.radio*sin(theta)
			arriba.append([x,y,self.altura/2])
			abajo.append([x,y,-(self.altura/2)])
			theta += dtheta
		for i in range(self.slices):
			plano = [arriba[i-1],arriba[i],abajo[i],abajo[i-1]]
			self.fig.planos.append(plano)
		self.fig.planos.append(arriba)
		self.fig.planos.append(abajo)
			
	def draw(self):
		self.fig.draw()

angulo = 0

def timer():
	global angulo
	angulo += 0.4
	glutPostRedisplay()
	#glutTimerFunc(10,timer,0)

def display():
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
	glColor3f(0.8,0.2,0.2)
	glPushMatrix()
	#glTranslatef(0.0,0.0,4)
	glRotatef(30,1,0,0)
	glRotatef(angulo,0,0,1)
	c = Cilindro(0.5,2,36)
	c.draw()
	#glutWireCube(1)
	glPopMatrix()
	glFlush()
	glutSwapBuffers()
	return
	
def init():
	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()
	glEnable(GL_DEPTH_TEST)
	glShadeModel(GL_SMOOTH)
	glDepthMask(GL_TRUE)
	glShadeModel(GL_SMOOTH)

		
	glEnable(GL_LIGHTING)
	glEnable(GL_LIGHT0)
	glLightfv(GL_LIGHT0, GL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
	glLightfv(GL_LIGHT0, GL_DIFFUSE, [0.8, 0.8, 0.8, 1.0])
	glLightfv(GL_LIGHT0, GL_SPECULAR,[0.5, 0.5, 0.5, 1.0])
	glLightfv(GL_LIGHT0, GL_POSITION,[1.5, 1.0,-4.0, 1.0])
		
	
if __name__ == '__main__':
	
	glutInit("mi cubito")
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
	glutInitWindowSize(500,500)
	glutInitWindowPosition(100,50)
	glutCreateWindow("mi primer dibujo")
	glClearColor(0.0,0.8,0.6,0.9)
	init()	
	glutDisplayFunc(display)
	#glutIdleFunc(timer)
	glutMainLoop()
